package edu.ncsu.csc.csc454.project.gui.info_panel;

import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;

import javax.swing.JPanel;

import edu.ncsu.csc.csc454.project.resources.Constants;
import edu.ncsu.csc.csc454.project.utilities.ImageUtilities;

/**
 * @author Team AW
 * @version 0.1
 */
@SuppressWarnings("serial")
public class BatteryIndicator extends JPanel {

    private BufferedImage[] batteries;

    private int             currentBattery;

    public BatteryIndicator() {
        initBatteries();
        initThread();
        setOpaque(false);
        setPreferredSize(new Dimension(batteries[0].getWidth(), batteries[0].getHeight()));
    }

    private void initBatteries() {
        batteries = new BufferedImage[Constants.BATTERIES.length];

        for (int i = 0; i < batteries.length; i++) {
            batteries[i] = ImageUtilities.resize(ImageUtilities.loadImage(Constants.BATTERIES[i]),
                    20, 20);
        }

        currentBattery = 0;
    }

    private void initThread() {
        Thread t = new BarSelector();
        t.start();
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);
        ((Graphics2D) g).drawImage(batteries[currentBattery], null, 0, 0);
    }

    private class BarSelector extends Thread {

        private long sleepTime = 5000;

        @Override
        public void run() {
            while (true) {
                try {
                    sleep(sleepTime);
                } catch (InterruptedException e) {}

                currentBattery++;

                if (currentBattery == 5) {
                    sleepTime = 500;
                } else if (currentBattery == 6) {
                    currentBattery = 4;
                }

                repaint();
            }
        }
    }
}
